package com.coze.lg.cozeagent.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.coze.lg.cozeagent.config.CozeConfig;
import com.coze.openapi.service.service.CozeAPI;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
@RestController
@Component

public class socketMySocket extends Thread {
    @Value("${port}")
    private int port;
    private final CozeAPI cozeAPI;
    // 初始化一个AipSpeech
    private final AipSpeech client;

    public socketMySocket(CozeAPI cozeAPI) {
        this.cozeAPI = cozeAPI;
        this.client = new AipSpeech();
    }

    @Override
    public void run() {
        try {
            // 创建ServerSocket对象，指定监听的端口号
            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("等待客户端连接...");

            while (true) {
                // 监听客户端的连接请求
                Socket clientSocket = serverSocket.accept();
                class NewThread extends Thread {
                    @Override
                    public void run() {
                        SessionMySocket sessionMySocket = new SessionMySocket(client, cozeAPI, clientSocket);
                        sessionMySocket.do_restart();
                    }
                };
                NewThread thread = new NewThread();
                thread.start();

                System.out.println("客户端已连接");
            }
        } catch (IOException e) {
            System.err.println("Socket错误: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("未预期的错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
